#!/bin/bash -e

CUR_DIR="$( cd "$( dirname "$0"  )" && pwd  )"

cd ${CUR_DIR}

while getopts "p:h" opt; do  
    case $opt in  
    p)  
        ROOM_ID=$OPTARG
        ;;  
    \?)  
        echo "Invalid option: -$OPTARG"
        echo "stop_game.sh -p roomid"
        exit
        ;;  
    esac  
done

if [ ${ROOM_ID} = 0 ]
then
   echo "roomID can be understand!"
   exit
fi

PID=$(ps -ef| grep "./bin/yxj -p ${ROOM_ID}" | grep -v grep | awk '{print $2}')
FILE_PID=$(cat ./proc/${ROOM_ID}/yxjmj.pid)
if [ -z ${PID} ]
then
    echo "pid ${PID} isn't opened"
    exit 0
fi
if [ -z ${FILE_PID} ]
then
    echo "file_pid ${FILE_PID} isn't opened"
    exit 0
fi

if [ ${FILE_PID} = ${PID} ]
then
    `kill ${PID}`
    echo "success"
    exit 1
else
    echo "file_pid ${FILE_PID} and pid ${PID} aren't same"
    exit 0
fi

